package com.chen.blue.service.system.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chen.blue.converter.system.LogConverter;
import com.chen.blue.domain.delete.system.LogDelete;
import com.chen.blue.domain.dto.system.LogDto;
import com.chen.blue.domain.model.system.LogModel;
import com.chen.blue.domain.model.system.PostModel;
import com.chen.blue.domain.query.system.LogQuery;
import com.chen.blue.domain.vo.excel.LogExcel;
import com.chen.blue.domain.vo.system.LogVo;
import com.chen.blue.exception.e.DtoNullException;
import com.chen.blue.mapper.system.LogMapper;
import com.chen.blue.result.DataResult;
import com.chen.blue.service.system.ILogService;
import com.chen.blue.utils.PageInfoUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author CLT
 * @since 2023-10-28
 */
@Service("logService")
public class ILogServiceImpl extends ServiceImpl<LogMapper, LogModel> implements ILogService {

    @Override
    public boolean addLog(LogDto logDto) throws DtoNullException {
        if (Objects.isNull(logDto)) {
            throw new DtoNullException("参数为空,不可添加");
        }

        return super.save(LogConverter.dtoToModel(logDto));
    }

    @Override
    public boolean deleteLog(LogDelete logDelete) throws DtoNullException {
        if (Objects.isNull(logDelete)) {
            throw new DtoNullException("参数为空,不可添加");
        }

        LambdaQueryWrapper<LogModel> wrapper = LogModel.wrapper;
        if (Objects.nonNull(logDelete.getId())) {
            wrapper.eq(true, LogModel::getId, logDelete.getId());
        }

        return super.remove(wrapper);
    }

    @Override
    public boolean updateLogById(LogDto logDto) throws DtoNullException {
        if (Objects.isNull(logDto)) {
            throw new DtoNullException("参数为空,不可添加");
        }

        return super.updateById(LogConverter.dtoToModel(logDto));
    }

    @Override
    public DataResult<List<LogVo>> queryLog(LogQuery logQuery) throws DtoNullException {

        if (Objects.isNull(logQuery)) {
            throw new DtoNullException("参数为空,不可添加");
        }

        /**
         * 条件查询
         */
        LambdaQueryWrapper<LogModel> wrapper = LogModel.wrapper;
        if (Objects.nonNull(logQuery.getPageNo()) && Objects.nonNull(logQuery.getPageSize())) {
            PageHelper.startPage(logQuery.getPageNo(), logQuery.getPageSize());
        }
        wrapper.orderByDesc(LogModel::getCreateTime);

        List<LogModel> logModelList = super.list(wrapper);
        PageInfo<LogModel> pageInfo = new PageInfo<>(logModelList);
        PageHelper.clearPage();

        /**
         * 统一返回
         */
        DataResult<List<LogVo>> dataResult = new DataResult<>();
        dataResult.setData(LogConverter.modelToVoList(pageInfo.getList()));
        PageInfoUtil.setPageInfo(dataResult, pageInfo);

        return dataResult;
    }


    @Override
    public DataResult<LogVo> queryLogById(Long id) throws DtoNullException {
        if (Objects.isNull(id)) {
            throw new DtoNullException("参数为空,不可添加");
        }

        LogModel LogModel = super.getById(id);

        DataResult<LogVo> dataResult = new DataResult<>();
        dataResult.setData(LogConverter.modelToVo(LogModel));

        return dataResult;
    }

    @Override
    public boolean include(List<LogExcel> logExcel) {

        List<LogModel> logModelList = LogConverter.excelToDto(logExcel);
        return super.saveBatch(logModelList);

    }

}
